package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.service.FormulaService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.formula.pojo.dto.FormulaDTO;
import com.xbongbong.pro.formula.pojo.dto.ValidateFormulaDTO;
import com.xbongbong.pro.formula.pojo.dto.WorkflowValidateFormulaDTO;
import com.xbongbong.pro.formula.pojo.vo.FormulaVO;
import com.xbongbong.pro.formula.pojo.vo.ValidateFormulaVO;
import com.xbongbong.pro.formula.pojo.vo.WorkflowValidateFormulaVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2019/5/30 11:03
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/formula")
public class FormulaController {

    @Resource
    private FormulaService formulaService;

    @RequestMapping(value = "/calculate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getActivityWeb(@RequestBody @Valid FormulaDTO formulaDTO, BindingResult br) {
        XbbResponse<FormulaVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FormulaVO getWebVO = formulaService.formulaCalculate(formulaDTO);
                response = new XbbResponse<>(getWebVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/validate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String validate(@RequestBody @Valid ValidateFormulaDTO validateFormulaDTO, BindingResult br) {
        XbbResponse<ValidateFormulaVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ValidateFormulaVO validate = formulaService.validate(validateFormulaDTO);
                response = new XbbResponse<>(validate);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    @RequestMapping(value = "/workflowValidate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String workflowValidate(@RequestBody @Valid WorkflowValidateFormulaDTO workflowValidateFormulaDTO, BindingResult br) {
        XbbResponse<WorkflowValidateFormulaVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowValidateFormulaVO workflowValidateFormulaVO = formulaService.workflowValidate(workflowValidateFormulaDTO);
                response = new XbbResponse<>(workflowValidateFormulaVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
